package com.sumer.psychology.microblog.application.command;

import com.sumer.psychology.microblog.domain.model.MicroBlog;
import com.sumer.psychology.microblog.domain.model.MicroBlogId;
import com.sumer.psychology.microblog.domain.model.MicroBlogRepository;
import org.springframework.transaction.annotation.Transactional;

/**
 * 微博服务
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/9/2
 * Time: 13:29
 */
@Transactional
public class MicroBlogApplicationService {
    private MicroBlogRepository microBlogRepository;

    public MicroBlogApplicationService(MicroBlogRepository microBlogRepository) {
        this.microBlogRepository = microBlogRepository;
    }

    /**
     * 新建一条微博
     *
     * @param createMicroBlogCommand command
     */
    public MicroBlogId newMicroBlog(CreateMicroBlogCommand createMicroBlogCommand) {
        MicroBlog aMicroBlog = new MicroBlog(
                microBlogRepository().nextIdentity(),
                createMicroBlogCommand.content(),
                createMicroBlogCommand.author());

        microBlogRepository().add(aMicroBlog);

        return aMicroBlog.microBlogId();
    }

    /**
     * 删除一条微博
     *
     * @param deleteMicroBlogCommand command
     */
    public void delete(DeleteMicroBlogCommand deleteMicroBlogCommand) {
        MicroBlog deletedMicroBlog = microBlogRepository().ofMicroBlogId(
                new MicroBlogId(deleteMicroBlogCommand.microBlogId()));

        if (deletedMicroBlog != null) {
            deletedMicroBlog.delete();
        }
    }

    private MicroBlogRepository microBlogRepository() {
        return microBlogRepository;
    }

}
